import axios from 'axios'

export const getAllCabinet = async () => {

    let result;
    await axios({
        method: 'get',
        url: '/api/cabinet'
    }).then((res) => {
        if(res.status == 200)
            result = res.data
        else {
            window.$message.error("请求机柜数据失败")
            result = false
        }      
    })
    return result
}

export const getCabinetById = async (id) => {

    let result;
    await axios({
        method: 'get',
        url: '/api/cabinet/' + id
    }).then((res) => {
        if(res.status == 200)
            result = res.data
        else {
            window.$message.error("请求机柜数据失败")
            result = false
        }
    })
    return result
}

export const addCabinet = async (data) => {

    let result;
    try{
        await axios({
            method: 'post',
            url: '/api/cabinet/',
            data,
        }).then((res) => { 
            if(res.status == 201) {
                window.$message.success("添加机柜成功")
                result = res.data
            }
            else {
                window.$message.error("添加机柜失败")
                result = false
            }
        })
    } 
    catch(err) {
        window.$message.error("添加机柜失败")
        result = false
    }
    finally {
        return result
    }
}

export const updateCabinet = async (_data) => {
    let result;
    try{ 
        await axios({
            method: 'put',
            url: '/api/cabinet/' + _data.id + '/',
            data: _data,
        }).then((res) => { 
            if(res.status == 200) {
                window.$message.success("修改机柜成功")
                result = res.data
            }
            else {
                window.$message.error("修改机柜失败")
                result = false
            }
        })
    }
    catch(err) {  
        window.$message.error("修改机柜失败")
        result = false
    }
    finally {
        return result
    } 
}

export const deleteCabinet = async (id) => {

    let result;
    try{
        await axios({
            method: 'delete',
            url: '/api/cabinet/' + id + '/',
        }).then((res) => {
            window.$message.success("删除机柜成功")
            result = res
        })
    }
    catch(err) {
        window.$message.error("添加机柜失败")
        result = false
    }
    finally {
        return result
    }
}



